/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LocalizarFornecedor.java
 *
 * Created on 27/04/2011, 00:38:38
 */
package localizarEditar;

import Classe.BD;
import Classe.Fornecedor;
import Classe.Validacao;
import Tela.CadastroPeca;
import editar.EditarFornecedor;
import editar.EditarPeca;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author gladson
 */
public class LocalizarFornecedor extends Validacao {

    /** Creates new form LocalizarFornecedor */
    public LocalizarFornecedor() {
        initComponents();
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        StxtOpcLocFornecedor.setEnabled(false);
        jPanel5.setVisible(false);
    }
    String SelecaoFornecedor;
    CadastroPeca peca;
    EditarPeca edpeca;
    int editar = 0;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbSelecaoFornecedor = new javax.swing.JComboBox();
        StxtOpcLocFornecedor = new javax.swing.JFormattedTextField();
        btPesquisarFornecedor = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TabelaFornecedor = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        itxtCodForn = new javax.swing.JTextField();
        btEditarFornecedor = new javax.swing.JButton();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        itxtFornecedor = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Localizar Fornecedor", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N
        jPanel3.setFont(new java.awt.Font("Arial", 1, 12));

        jLabel1.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel1.setText("Localizar  Por:");

        cbSelecaoFornecedor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "", "Nome Fantasia", "Razão Social", "CNPJ","Insc.Estadual","Data Cadastro" }));
        cbSelecaoFornecedor.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        cbSelecaoFornecedor.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbSelecaoFornecedorItemStateChanged(evt);
            }
        });

        StxtOpcLocFornecedor.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        btPesquisarFornecedor.setText("Pesquisar");
        btPesquisarFornecedor.setToolTipText("Pesquisar");
        btPesquisarFornecedor.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btPesquisarFornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPesquisarFornecedorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(StxtOpcLocFornecedor, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(cbSelecaoFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(btPesquisarFornecedor)
                .addContainerGap(389, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbSelecaoFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StxtOpcLocFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPesquisarFornecedor))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        TabelaFornecedor.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Data Cadastro", "Código", "Nome Fantásia", "Razão Social", "CNPJ", "Insc.Estadual", "Contato", "Telefone"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(TabelaFornecedor);

        jButton1.setText("Sair");
        jButton1.setToolTipText("Sair");
        jButton1.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel2.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel2.setText("Código Fornecedor");

        itxtCodForn.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEtchedBorder(), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));

        btEditarFornecedor.setText("Editar Fornecedor");
        btEditarFornecedor.setToolTipText("Editar Fornecedor");
        btEditarFornecedor.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        btEditarFornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditarFornecedorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(itxtCodForn, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btEditarFornecedor)
                .addContainerGap(34, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(itxtCodForn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btEditarFornecedor))
                .addContainerGap())
        );

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel3.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel3.setText("Código Fornecedor");

        itxtFornecedor.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEtchedBorder(), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));

        jButton2.setText("Inserir Fornecedor");
        jButton2.setToolTipText("Inserir Fornecedor");
        jButton2.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(itxtFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addContainerGap(39, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(itxtFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(7, 7, 7))
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 302, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-808)/2, (screenSize.height-627)/2, 808, 627);
    }// </editor-fold>//GEN-END:initComponents

    private void btPesquisarFornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPesquisarFornecedorActionPerformed
        if (cbSelecaoFornecedor.getSelectedItem().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Selecione uma opção para consulta!!");
        } else {
            if (validarLocalizador(StxtOpcLocFornecedor.getText())) {
                String Opcao = ((String) cbSelecaoFornecedor.getSelectedItem());
                boolean esc = false;
                try {
                    if (Opcao.equalsIgnoreCase("Nome Fantasia")) {
                        SelecaoFornecedor = "NOME_FANT_FORNECEDOR LIKE '%";
                        esc = true;
                    }
                    if (Opcao.equalsIgnoreCase("Razão Social")) {
                        SelecaoFornecedor = "RAZAO_FORNECEDOR LIKE '%";
                        esc = true;
                    }
                    if (Opcao.equalsIgnoreCase("CNPJ")) {
                        SelecaoFornecedor = "CNPJ_FORNECEDOR";
                        esc = false;
                    }
                    if (Opcao.equalsIgnoreCase("Insc.Estadual")) {
                        SelecaoFornecedor = "INSC_EST_FORNECEDOR LIKE '%";
                        esc = true;
                    }
                    if (Opcao.equalsIgnoreCase("Data Cadastro")) {
                        SelecaoFornecedor = "DATA_CADAS_FORNECEDOR";
                        esc = false;
                    }


                    String sql = "SELECT * FROM FORNECEDOR WHERE " + SelecaoFornecedor + "='" + StxtOpcLocFornecedor.getText().trim().toUpperCase() + "'";
                    if (esc) {
                        String sql2 = "SELECT * FROM FORNECEDOR WHERE " + SelecaoFornecedor + "" + StxtOpcLocFornecedor.getText().trim().toUpperCase() + "%'";
                        sql = sql2;
                    }

                    BD banco = new BD();
                    ResultSet res = banco.consultar(sql);

                    DefaultTableModel model = (DefaultTableModel) TabelaFornecedor.getModel();
                    model.setNumRows(0);
                    while (res.next()) {
                        //---------------------------------formatando a data de cadastro---------------------------

                        String data2 = res.getString("DATA_CADAS_FORNECEDOR");
                        String dtFinalC = "";
                        if (data2 != null) {
                            String dataC = data2.split(" ")[0];  //corta a strin onde tem o espaço
                            String[] vDataC = dataC.split("-");//corta a onde tem "-"
                            String diaC = vDataC[2];
                            String mesC = vDataC[1];
                            String anoC = vDataC[0];
                            dtFinalC = diaC + "/" + mesC + "/" + anoC;
                        }

//---------------------------------fim da formatação da data--------------------------------
                        Object[] linha = {
                            dtFinalC,
                            res.getInt("ID_FORNECEDOR"),
                            res.getString("NOME_FANT_FORNECEDOR"),
                            res.getString("RAZAO_FORNECEDOR"),
                            res.getString("CNPJ_FORNECEDOR"),
                            res.getString("INSC_EST_FORNECEDOR"),
                            res.getString("CONTATO_FORNECEDOR"),
                            res.getString("TEL_FORNECEDOR")};
                        model.addRow(linha);
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }//GEN-LAST:event_btPesquisarFornecedorActionPerformed
    MaskFormatter mascara = new MaskFormatter();
    private void cbSelecaoFornecedorItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbSelecaoFornecedorItemStateChanged

        StxtOpcLocFornecedor.setEnabled(true);
        DefaultTableModel model = (DefaultTableModel) TabelaFornecedor.getModel();
        model.setNumRows(0);

        if (cbSelecaoFornecedor.getSelectedItem() == "CNPJ") {
            try {
                mascara.setMask("##.###.###/####-##");
                mascara.setPlaceholderCharacter(' ');
                mascara.install(StxtOpcLocFornecedor);
            } catch (ParseException ex) {
                Logger.getLogger(LocalizarFornecedor.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (cbSelecaoFornecedor.getSelectedItem() == "Data Cadastro") {
            try {
                mascara.setMask("##/##/####");
                mascara.setPlaceholderCharacter(' ');
                mascara.install(StxtOpcLocFornecedor);
            } catch (ParseException ex) {
                Logger.getLogger(LocalizarFornecedor.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {

            mascara.uninstall();
            StxtOpcLocFornecedor.setText("");
        }

    }//GEN-LAST:event_cbSelecaoFornecedorItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void btEditarFornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditarFornecedorActionPerformed
        if (validarNumero(itxtCodForn.getText())) {
            int codForn = Integer.parseInt(itxtCodForn.getText().trim());

            String sql = "SELECT * FROM FORNECEDOR WHERE ID_FORNECEDOR=" + codForn;
            BD bd = new BD();
            ResultSet r1;
            try {
                r1 = bd.consultar(sql);
                if (r1.next()) {
                    EditarFornecedor forn = new EditarFornecedor(codForn);
                    forn.setVisible(true);
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Fornecedor Não encontrado na base de dados");
                    itxtCodForn.setText("");
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(LocalizarFornecedor.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(LocalizarFornecedor.class.getName()).log(Level.SEVERE, null, ex);
            }



        }
    }//GEN-LAST:event_btEditarFornecedorActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (validarNumero(itxtFornecedor.getText())) {

            if (editar == 1) {
                try {
                    int id_forn = Integer.parseInt(itxtFornecedor.getText().trim());
                    String sql = "SELECT * FROM FORNECEDOR WHERE ID_FORNECEDOR = " + id_forn;

                    BD banco = new BD();
                    ResultSet res = banco.consultar(sql);

                    if (res.next()) {
                        Fornecedor form = new Fornecedor();
                        form.setIdFornecedor(res.getInt("ID_FORNECEDOR"));
                        form.setRazaoSocialFornecedor(res.getString("RAZAO_FORNECEDOR"));
                        edpeca.preecherForn(form);
                        this.dispose();
                    } else {
                        JOptionPane.showMessageDialog(rootPane, "Fornecedor Não encontrado na base de dados");
                        itxtFornecedor.setText("");
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {

                try {
                    int id_forn = Integer.parseInt(itxtFornecedor.getText().trim());
                    String sql = "SELECT * FROM FORNECEDOR WHERE ID_FORNECEDOR = " + id_forn;

                    BD banco = new BD();
                    ResultSet res = banco.consultar(sql);

                    if (res.next()) {
                        Fornecedor form = new Fornecedor();
                        form.setIdFornecedor(res.getInt("ID_FORNECEDOR"));
                        form.setRazaoSocialFornecedor(res.getString("RAZAO_FORNECEDOR"));
                        peca.preecherForn(form);
                        this.dispose();
                    } else {
                        JOptionPane.showMessageDialog(rootPane, "Fornecedor Não encontrado na base de dados");
                        itxtFornecedor.setText("");
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }

    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LocalizarFornecedor().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField StxtOpcLocFornecedor;
    private javax.swing.JTable TabelaFornecedor;
    private javax.swing.JButton btEditarFornecedor;
    private javax.swing.JButton btPesquisarFornecedor;
    private javax.swing.JComboBox cbSelecaoFornecedor;
    private javax.swing.JTextField itxtCodForn;
    private javax.swing.JTextField itxtFornecedor;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    public void recebePeca(CadastroPeca pec) {
        this.peca = pec;
        jPanel5.setVisible(true);
        jPanel4.setVisible(false);
    }

    public void recebePeca(EditarPeca aThis) {
        editar = 1;
        this.edpeca = aThis;
        jPanel5.setVisible(true);
        jPanel4.setVisible(false);
    }
}
